/****************************************************************************
 * libwiigui Template
 * Tantric 2009
 *
 * menu.cpp
 * Menu flow routines - handles all menu logic
 ***************************************************************************/

#include <gccore.h>
#include <ogcsys.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <wiiuse/wpad.h>
#include <fat.h>
#include <sdcard/wiisd_io.h>
#include <sys/stat.h>
#include <time.h>

#include "libwiigui/gui.h"
#include "menu.h"
#include "main.h"
#include "input.h"
#include "filelist.h"
#include "loader/processor.h"
#include "loader/forward.h"
#include "loader/game.h"
#include "chan/channel.h"



const int slot1x = 40;
const int slot1y = 40;
static GuiImageData * pointer[4];
static GuiImage * bgImg = NULL;
static GuiSound * bgMusic = NULL;
static GuiImageData * background = NULL;
static GuiWindow * mainWindow = NULL;
static lwp_t guithread = LWP_THREAD_NULL;
static bool guiHalt = true;
char* path = NULL;
char* filename = NULL;
int load = 0;
const char* conf = "SD:/guif/dash.conf";
int page = 1;
char imgPath[100];
s32 selected;
int appload = 0;
channel *channel_list;
u32 channel_cnt;
//static int slotx[10] = {184, 338, 492, 184, 338, 492, 30, 184, 338, 492};
//static int sloty[10] = {30, 30, 30, 133, 133, 133, 236, 236, 236, 236}; 


int file_exists (char * fileName)
{
   struct stat buf;
   int i = stat ( fileName, &buf );
     /* File found */
     if ( i == 0 )
     {
       return 1;
     }
     return 0;
       
}




/****************************************************************************
 * ResumeGui
 *
 * Signals the GUI thread to start, and resumes the thread. This is called
 * after finishing the removal/insertion of new elements, and after initial
 * GUI setup.
 ***************************************************************************/
static void
ResumeGui()
{
	guiHalt = false;
	LWP_ResumeThread (guithread);
}

/****************************************************************************
 * HaltGui
 *
 * Signals the GUI thread to stop, and waits for GUI thread to stop
 * This is necessary whenever removing/inserting new elements into the GUI.
 * This eliminates the possibility that the GUI is in the middle of accessing
 * an element that is being changed.
 ***************************************************************************/
static void
HaltGui()
{
	guiHalt = true;

	// wait for thread to finish
	while(!LWP_ThreadIsSuspended(guithread))
		usleep(50);
}



/****************************************************************************
 * WindowPrompt
 *
 * Displays a prompt window to user, with information, an error message, or
 * presenting a user with a choice
 ***************************************************************************/
int
WindowPrompt(const char *title, const char *msg, const char *btn1Label, const char *btn2Label)
{
	
	snprintf(imgPath, sizeof(imgPath), "%sbutton.png", Settings[0].ThemePath);
	GuiImageData btnOutline(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbutton_over.png", Settings[0].ThemePath);
	GuiImageData btnOutlineOver(imgPath, 0);
	
	int choice = -1;
	char dialogpath[100];
	GuiWindow promptWindow(448,288);
	promptWindow.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
	promptWindow.SetPosition(0, -10);
	GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, SOUND_PCM);
	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);

	snprintf(dialogpath, sizeof(dialogpath), "%sdialogue_box.png", Settings[0].ThemePath);
	GuiImageData dialogBox(dialogpath, 0);
	GuiImage dialogBoxImg(&dialogBox);

	GuiText titleTxt(title, 26, (GXColor){255, 255, 255, 255});
	titleTxt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
	titleTxt.SetPosition(0,40);
	GuiText msgTxt(msg, 22, (GXColor){255, 255, 255, 255});
	msgTxt.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
	msgTxt.SetPosition(0,-20);
	msgTxt.SetMaxWidth(430);

	GuiText btn1Txt(btn1Label, 22, (GXColor){0, 0, 0, 255});
	GuiImage btn1Img(&btnOutline);
	GuiImage btn1ImgOver(&btnOutlineOver);
	GuiButton btn1(btnOutline.GetWidth(), btnOutline.GetHeight());

	if(btn2Label)
	{
		btn1.SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
		btn1.SetPosition(20, -25);
	}
	else
	{
		btn1.SetAlignment(ALIGN_CENTRE, ALIGN_BOTTOM);
		btn1.SetPosition(0, -25);
	}

	btn1.SetLabel(&btn1Txt);
	btn1.SetImage(&btn1Img);
	btn1.SetImageOver(&btn1ImgOver);
	btn1.SetSoundOver(&btnSoundOver);
	btn1.SetTrigger(&trigA);
	btn1.SetState(STATE_SELECTED);
	btn1.SetEffectGrow();

	GuiText btn2Txt(btn2Label, 22, (GXColor){0, 0, 0, 255});
	GuiImage btn2Img(&btnOutline);
	GuiImage btn2ImgOver(&btnOutlineOver);
	GuiButton btn2(btnOutline.GetWidth(), btnOutline.GetHeight());
	btn2.SetAlignment(ALIGN_RIGHT, ALIGN_BOTTOM);
	btn2.SetPosition(-20, -25);
	btn2.SetLabel(&btn2Txt);
	btn2.SetImage(&btn2Img);
	btn2.SetImageOver(&btn2ImgOver);
	btn2.SetSoundOver(&btnSoundOver);
	btn2.SetTrigger(&trigA);
	btn2.SetEffectGrow();

	promptWindow.Append(&dialogBoxImg);
	promptWindow.Append(&titleTxt);
	promptWindow.Append(&msgTxt);
	promptWindow.Append(&btn1);

	if(btn2Label)
		promptWindow.Append(&btn2);

	promptWindow.SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_IN, 50);
	HaltGui();
	mainWindow->SetState(STATE_DISABLED);
	mainWindow->Append(&promptWindow);
	mainWindow->ChangeFocus(&promptWindow);
	ResumeGui();

	while(choice == -1)
	{
		VIDEO_WaitVSync();

		if(btn1.GetState() == STATE_CLICKED)
			choice = 1;
		else if(btn2.GetState() == STATE_CLICKED)
			choice = 0;
	}

	promptWindow.SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_OUT, 50);
	while(promptWindow.GetEffect() > 0) usleep(50);
	HaltGui();
	mainWindow->Remove(&promptWindow);
	mainWindow->SetState(STATE_DEFAULT);
	ResumeGui();
	return choice;
}


/****************************************************************************
 * UpdateGUI
 *
 * Primary thread to allow GUI to respond to state changes, and draws GUI
 ***************************************************************************/

static void *
UpdateGUI (void *arg)
{
	while(1)
	{
		if(guiHalt)
		{
			LWP_SuspendThread(guithread);
		}
		else
		{
			mainWindow->Draw();

			#ifdef HW_RVL
			for(int i=3; i >= 0; i--) // so that player 1's cursor appears on top!
			{
				if(userInput[i].wpad.ir.valid)
					Menu_DrawImg(userInput[i].wpad.ir.x-48, userInput[i].wpad.ir.y-48,
						96, 96, pointer[i]->GetImage(), userInput[i].wpad.ir.angle, 1, 1, 255);
				DoRumble(i);
			}
			#endif

			Menu_Render();

			for(int i=0; i < 4; i++)
				mainWindow->Update(&userInput[i]);

			if(ExitRequested)
			{
				for(int a = 0; a < 255; a += 15)
				{
					mainWindow->Draw();
					Menu_DrawRectangle(0,0,screenwidth,screenheight,(GXColor){0, 0, 0, a},1);
					Menu_Render();
				}
				ExitApp();
				int ret = 0;
				if (load == 1){
					ret = forward(path);
				} else if (load == 2){
					ret = LoadGame();
				} else if (load == 3) {
					Channel_Launch(selected);
				} else if (load == 0){
					exit(0);
				}
				if (ret < 0) {
					load = 0;
					ExitRequested=0;
					InitGUIThreads();
					FirstMenu(MENU_MAIN);
				} else {
				exit(0); 
				}
			}
		}
	}
	return NULL;
}

/****************************************************************************
 * InitGUIThread
 *
 * Startup GUI threads
 ***************************************************************************/
void
InitGUIThreads()
{
	LWP_CreateThread (&guithread, UpdateGUI, NULL, NULL, 0, 70);
}

/****************************************************************************
 * OnScreenKeyboard
 *
 * Opens an on-screen keyboard window, with the data entered being stored
 * into the specified variable.
 ***************************************************************************/
static void OnScreenKeyboard(char * var, u16 maxlen)
{
	
	int save = -1;

	GuiKeyboard keyboard(var, maxlen);

	GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, SOUND_PCM);
	snprintf(imgPath, sizeof(imgPath), "%sbutton.png", Settings[0].ThemePath);
	GuiImageData btnOutline(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbutton_over.png", Settings[0].ThemePath);
	GuiImageData btnOutlineOver(imgPath, 0);
	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);

	GuiText okBtnTxt("OK", 22, (GXColor){0, 0, 0, 255});
	GuiImage okBtnImg(&btnOutline);
	GuiImage okBtnImgOver(&btnOutlineOver);
	GuiButton okBtn(btnOutline.GetWidth(), btnOutline.GetHeight());

	okBtn.SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
	okBtn.SetPosition(25, -25);

	okBtn.SetLabel(&okBtnTxt);
	okBtn.SetImage(&okBtnImg);
	okBtn.SetImageOver(&okBtnImgOver);
	okBtn.SetSoundOver(&btnSoundOver);
	okBtn.SetTrigger(&trigA);
	okBtn.SetEffectGrow();

	GuiText cancelBtnTxt("Cancel", 22, (GXColor){0, 0, 0, 255});
	GuiImage cancelBtnImg(&btnOutline);
	GuiImage cancelBtnImgOver(&btnOutlineOver);
	GuiButton cancelBtn(btnOutline.GetWidth(), btnOutline.GetHeight());
	cancelBtn.SetAlignment(ALIGN_RIGHT, ALIGN_BOTTOM);
	cancelBtn.SetPosition(-25, -25);
	cancelBtn.SetLabel(&cancelBtnTxt);
	cancelBtn.SetImage(&cancelBtnImg);
	cancelBtn.SetImageOver(&cancelBtnImgOver);
	cancelBtn.SetSoundOver(&btnSoundOver);
	cancelBtn.SetTrigger(&trigA);
	cancelBtn.SetEffectGrow();

	keyboard.Append(&okBtn);
	keyboard.Append(&cancelBtn);

	HaltGui();
	mainWindow->SetState(STATE_DISABLED);
	mainWindow->Append(&keyboard);
	mainWindow->ChangeFocus(&keyboard);
	ResumeGui();

	while(save == -1)
	{
		VIDEO_WaitVSync();

		if(okBtn.GetState() == STATE_CLICKED)
			save = 1;
		else if(cancelBtn.GetState() == STATE_CLICKED)
			save = 0;
	}

	if(save)
	{
		snprintf(var, maxlen, "%s", keyboard.kbtextstr);
	}

	HaltGui();
	mainWindow->Remove(&keyboard);
	mainWindow->SetState(STATE_DEFAULT);
	ResumeGui();
}

/****************************************************************************
 * ManagePrompt
 *
 * Brings up a display to manage the Application that was clicked on.
 * You can move up, move down or delete from list.
 ***************************************************************************/
int
ManagePrompt(char *AppName, char *AppPath, char *AppFile)
{
	
	int choice = -1;
	int appnum = 0;
	int maxnum = 0;
	char dialogpath[100];
	while (!strstr(Settings[maxnum].AppName, "-1"))
		maxnum++;
	while (!strstr(Settings[appnum].AppName, AppName))
		appnum++;
	maxnum--;
	
	snprintf(imgPath, sizeof(imgPath), "%sbutton.png", Settings[0].ThemePath);
	GuiImageData btnOutline(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbutton_over.png", Settings[0].ThemePath);
	GuiImageData btnOutlineOver(imgPath, 0);
	GuiWindow promptWindow(448,288);
	promptWindow.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
	promptWindow.SetPosition(0, -10);
	GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, SOUND_PCM);
	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
	
	snprintf(dialogpath, sizeof(dialogpath), "%sdialogue_box.png", Settings[0].ThemePath);
	GuiImageData dialogBox(dialogpath, 0);
	GuiImage dialogBoxImg(&dialogBox);
	dialogBoxImg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
	dialogBoxImg.SetPosition(0,-10);

	GuiText nameTxt(AppName, 28, (GXColor){255, 255, 255, 255});
	GuiButton nameBtn(120,50);
	nameBtn.SetLabel(&nameTxt);
	nameBtn.SetLabelOver(&nameTxt);
	nameBtn.SetTrigger(&trigA);
	nameBtn.SetAlignment(ALIGN_CENTRE, ALIGN_BOTTOM);
	nameBtn.SetPosition(0,-230);
	nameBtn.SetEffectGrow();
	
	
	GuiText pathTxt(AppPath, 24, (GXColor){255, 255, 255, 255});
	GuiButton pathBtn(120,50);
	pathBtn.SetLabel(&pathTxt);
	pathBtn.SetLabelOver(&pathTxt);
	pathBtn.SetTrigger(&trigA);
	pathBtn.SetAlignment(ALIGN_CENTRE, ALIGN_BOTTOM);
	pathBtn.SetPosition(0,-185);
	pathBtn.SetEffectGrow();
	
	GuiText fileTxt(AppFile, 24, (GXColor){255, 255, 255, 255});
	GuiButton fileBtn(120,50);
	fileBtn.SetLabel(&fileTxt);
	fileBtn.SetLabelOver(&fileTxt);
	fileBtn.SetTrigger(&trigA);
	fileBtn.SetAlignment(ALIGN_CENTRE, ALIGN_BOTTOM);
	fileBtn.SetPosition(0, -140);
	fileBtn.SetEffectGrow();
	
	GuiText upTxt("Move UP", 22, (GXColor){0, 0, 0, 255});
	GuiImage upImg(&btnOutline);
	GuiImage upImgOver(&btnOutlineOver);
	GuiButton up(btnOutline.GetWidth(), btnOutline.GetHeight());
	up.SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
	up.SetPosition(35, -70);
	up.SetLabel(&upTxt);
	up.SetImage(&upImg);
	up.SetImageOver(&upImgOver);
	up.SetSoundOver(&btnSoundOver);
	up.SetTrigger(&trigA);
	up.SetState(STATE_SELECTED);
	up.SetEffectGrow();


	GuiText downTxt("Move Down", 22, (GXColor){0, 0, 0, 255});
	GuiImage downImg(&btnOutline);
	GuiImage downImgOver(&btnOutlineOver);
	GuiButton down(btnOutline.GetWidth(), btnOutline.GetHeight());
	down.SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
	down.SetPosition(35, -25);
	down.SetLabel(&downTxt);
	down.SetImage(&downImg);
	down.SetImageOver(&downImgOver);
	down.SetSoundOver(&btnSoundOver);
	down.SetTrigger(&trigA);
	down.SetEffectGrow();

	
	GuiText delTxt("Delete", 22, (GXColor){0, 0, 0, 255});
	GuiImage delImg(&btnOutline);
	GuiImage delImgOver(&btnOutlineOver);
	GuiButton del(btnOutline.GetWidth(), btnOutline.GetHeight());
	del.SetAlignment(ALIGN_RIGHT, ALIGN_BOTTOM);
	del.SetPosition(-35, -70);
	del.SetLabel(&delTxt);
	del.SetImage(&delImg);
	del.SetImageOver(&delImgOver);
	del.SetSoundOver(&btnSoundOver);
	del.SetTrigger(&trigA);
	del.SetEffectGrow();
	
	GuiText okTxt("OK", 22, (GXColor){0, 0, 0, 255});
	GuiImage okImg(&btnOutline);
	GuiImage okImgOver(&btnOutlineOver);
	GuiButton ok(btnOutline.GetWidth(), btnOutline.GetHeight());
	ok.SetAlignment(ALIGN_RIGHT, ALIGN_BOTTOM);
	ok.SetPosition(-35, -25);
	ok.SetLabel(&okTxt);
	ok.SetImage(&okImg);
	ok.SetImageOver(&okImgOver);
	ok.SetSoundOver(&btnSoundOver);
	ok.SetTrigger(&trigA);
	ok.SetEffectGrow();

	promptWindow.Append(&nameBtn);
	promptWindow.Append(&pathBtn);
	promptWindow.Append(&fileBtn);
	if (appnum != 0)
		promptWindow.Append(&up);
	if (appnum != maxnum)
		promptWindow.Append(&down);
	promptWindow.Append(&del);
	promptWindow.Append(&ok);

	HaltGui();
	mainWindow->SetState(STATE_DISABLED);
	mainWindow->Append(&dialogBoxImg);
	mainWindow->Append(&promptWindow);
	mainWindow->ChangeFocus(&promptWindow);
	ResumeGui();

	while(choice == -1)
	{
		VIDEO_WaitVSync();

		if(up.GetState() == STATE_CLICKED)
		{
			int b = appnum - 1;
			sprintf (Settings[appnum].TempName, "%s", Settings[b].AppName);
			sprintf (Settings[b].TempName, "%s", Settings[appnum].AppName);
			sprintf (Settings[appnum].AppName, "%s", Settings[appnum].TempName);
			sprintf (Settings[b].AppName, "%s", Settings[b].TempName);
			sprintf (Settings[appnum].TempPath, "%s", Settings[b].AppPath);
			sprintf (Settings[b].TempPath, "%s", Settings[appnum].AppPath);
			sprintf (Settings[appnum].AppPath, "%s", Settings[appnum].TempPath);
			sprintf (Settings[b].AppPath, "%s", Settings[b].TempPath);
			sprintf (Settings[appnum].TempFile, "%s", Settings[b].AppFile);
			sprintf (Settings[b].TempFile, "%s", Settings[appnum].AppFile);
			sprintf (Settings[appnum].AppFile, "%s", Settings[appnum].TempFile);
			sprintf (Settings[b].AppFile, "%s", Settings[b].TempFile);
			WriteConfig(conf);
			choice = 1;
		}
		else if(down.GetState() == STATE_CLICKED)
		{
			
			int b = appnum + 1;
			sprintf (Settings[appnum].TempName, "%s", Settings[b].AppName);
			sprintf (Settings[b].TempName, "%s", Settings[appnum].AppName);
			sprintf (Settings[appnum].AppName, "%s", Settings[appnum].TempName);
			sprintf (Settings[b].AppName, "%s", Settings[b].TempName);
			sprintf (Settings[appnum].TempPath, "%s", Settings[b].AppPath);
			sprintf (Settings[b].TempPath, "%s", Settings[appnum].AppPath);
			sprintf (Settings[appnum].AppPath, "%s", Settings[appnum].TempPath);
			sprintf (Settings[b].AppPath, "%s", Settings[b].TempPath);
			sprintf (Settings[appnum].TempFile, "%s", Settings[b].AppFile);
			sprintf (Settings[b].TempFile, "%s", Settings[appnum].AppFile);
			sprintf (Settings[appnum].AppFile, "%s", Settings[appnum].TempFile);
			sprintf (Settings[b].AppFile, "%s", Settings[b].TempFile);
			WriteConfig(conf);
			choice = 1;
		}
		else if(del.GetState() == STATE_CLICKED)
		{
			int b = appnum + 1;
			while (!strstr(Settings[appnum].AppName, "-1"))
			{
				sprintf (Settings[appnum].AppName, "%s", Settings[b].AppName);
				sprintf (Settings[appnum].AppPath, "%s", Settings[b].AppPath);
				sprintf (Settings[appnum].AppFile, "%s", Settings[b].AppFile);
				appnum++;
				b++;
			}
			sprintf (Settings[appnum].AppName, "-1");
				
			WriteConfig(conf);
			choice = 1;
		}
		else if (nameBtn.GetState() == STATE_CLICKED)
		{
			OnScreenKeyboard(Settings[appnum].AppName, 256);
			WriteConfig(conf);
			nameTxt.SetText(Settings[appnum].AppName);
		}
		else if (pathBtn.GetState() == STATE_CLICKED)
		{
			OnScreenKeyboard(Settings[appnum].AppPath, 256);
			WriteConfig(conf);
			pathTxt.SetText(Settings[appnum].AppPath);
		}
		else if (fileBtn.GetState() == STATE_CLICKED)
		{
			OnScreenKeyboard(Settings[appnum].AppFile, 256);
			WriteConfig(conf);
			fileTxt.SetText(Settings[appnum].AppFile);
		}
		else if (ok.GetState() == STATE_CLICKED)
		{
			choice = 1;
		}
	}

	HaltGui();
	mainWindow->Remove(&dialogBoxImg);
	mainWindow->Remove(&promptWindow);
	mainWindow->SetState(STATE_DEFAULT);
	ResumeGui();
	return choice;
}


/****************************************************************************
 * Main Menu
 ***************************************************************************/
static int MainMenu()
{
	
	int menu = MENU_NONE;
	
	char curtime [80];
	char curdate [80];
	int mintest;
	int daytest;
	
	int i = 0;
	while (!strstr(Settings[i].AppName, "-1")) {
		i++;
	}
	int q = i;
	int numpages = 1;
	while (q > 9) {
		q = q-9;
		numpages++;
	}
	
	snprintf(imgPath, sizeof(imgPath), "%sbutton.png", Settings[0].ThemePath);
	GuiImageData btnOutline(imgPath, 0);	
	snprintf(imgPath, sizeof(imgPath), "%sbutton_over.png", Settings[0].ThemePath);
	GuiImageData btnOutlineOver(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbutton_large.png", Settings[0].ThemePath);
	GuiImageData btnLargeOutline(imgPath, 0);	
	snprintf(imgPath, sizeof(imgPath), "%sbutton_large_over.png", Settings[0].ThemePath);
	GuiImageData btnLargeOutlineOver(imgPath, 0);		
	snprintf(imgPath, sizeof(imgPath), "%sbattery_white.png", Settings[0].ThemePath);
	GuiImageData battery(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbattery_red.png", Settings[0].ThemePath);
	GuiImageData batteryRed(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbattery_bar_white.png", Settings[0].ThemePath);
	GuiImageData batteryBar(imgPath, 0);
		
	
	time_t rawtime;
	struct tm * timeinfo;

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	mintest = timeinfo->tm_min;
	daytest = timeinfo->tm_wday;
	
	strftime (curtime,80,"%I:%M %p",timeinfo);
	
	GuiText timeTxt(curtime, 35, (GXColor){255,255,255,255});
	timeTxt.SetAlignment(ALIGN_CENTRE, ALIGN_BOTTOM);
	timeTxt.SetPosition(0,-95);
	
	strftime (curdate,80,"%a %m/%d",timeinfo);
	
	GuiText dateTxt(curdate, 35, (GXColor){0, 0, 0, 255});
	dateTxt.SetAlignment(ALIGN_CENTRE, ALIGN_BOTTOM);
	dateTxt.SetPosition(0, -30);
	
	snprintf(imgPath, sizeof(imgPath), "%siconback.png", Settings[0].ThemePath);
	GuiImageData iconback(imgPath, 0);
	GuiImage icon(&iconback);
	
	GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, SOUND_PCM);
	snprintf(imgPath, sizeof(imgPath), "%smenu_button.png", Settings[0].ThemePath);
	GuiImageData btnExit(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%smenu_button_over.png", Settings[0].ThemePath);
	GuiImageData btnExitOver(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%ssettings_button.png", Settings[0].ThemePath);
	GuiImageData btnSettings(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%ssettings_button_over.png", Settings[0].ThemePath);
	GuiImageData btnSettingsOver(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sdvd.png", Settings[0].ThemePath);
	GuiImageData btnDVD(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sdvd_over.png", Settings[0].ThemePath);
	GuiImageData btnDVDOver(imgPath, 0);


	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
	GuiTrigger trigHome;
	trigHome.SetButtonOnlyTrigger(-1, WPAD_BUTTON_HOME | WPAD_CLASSIC_BUTTON_HOME, 0);
		
	snprintf(imgPath, sizeof(imgPath), "%splus.png", Settings[0].ThemePath);
	GuiImageData btnplus(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%splus_over.png", Settings[0].ThemePath);
	GuiImageData btnplusover(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sminus.png", Settings[0].ThemePath);
	GuiImageData btnminus(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sminus_over.png", Settings[0].ThemePath);
	GuiImageData btnminusover(imgPath, 0);
		
	GuiTrigger trigminus;
	trigminus.SetButtonOnlyTrigger(-1, WPAD_BUTTON_MINUS | WPAD_CLASSIC_BUTTON_MINUS, 0);
	GuiTrigger trigplus;
	trigplus.SetButtonOnlyTrigger(-1, WPAD_BUTTON_PLUS | WPAD_CLASSIC_BUTTON_PLUS, 0);
		
	GuiImage plusBtnImg(&btnplus);
	GuiImage plusBtnImgOver(&btnplusover);
	GuiButton plusBtn(btnplus.GetWidth(), btnplus.GetHeight());
	plusBtn.SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
	plusBtn.SetPosition(-25, 200);
	plusBtn.SetImage(&plusBtnImg);
	plusBtn.SetImageOver(&plusBtnImgOver);
	plusBtn.SetSoundOver(&btnSoundOver);
	plusBtn.SetTrigger(&trigA);
	plusBtn.SetTrigger(&trigplus);
	plusBtn.SetEffectGrow();
		
	GuiImage minusBtnImg(&btnminus);
	GuiImage minusBtnImgOver(&btnminusover);
	GuiButton minusBtn(btnminus.GetWidth(), btnminus.GetHeight());
	minusBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	minusBtn.SetPosition(25, 200);
	minusBtn.SetImage(&minusBtnImg);
	minusBtn.SetImageOver(&minusBtnImgOver);
	minusBtn.SetSoundOver(&btnSoundOver);
	minusBtn.SetTrigger(&trigA);
	minusBtn.SetTrigger(&trigminus);
	minusBtn.SetEffectGrow();
	

	int a = 0;
	a = (page-1)*10;
	int b = (i%10);
	if (b == 0 && i != 0)
		b = 10;
		
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App1Icon(imgPath, 0);
	int ret1 = file_exists(imgPath);	
	GuiText App1BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App1BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App1BtnImg(&btnLargeOutline);
	GuiImage App1BtnImgOver(&btnLargeOutlineOver);
	GuiImage App1Iconimg(&App1Icon);
	GuiImage App1IconimgOver(&App1Icon);
	GuiButton App1Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App1Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App1Btn.SetPosition(174, 30);
	App1Btn.SetImage(&App1BtnImg);
	if (page == numpages && b > a)
	{
		if (ret1 == 0)
		{
			App1Btn.SetLabel(&App1BtnTxt);
		} else {
			App1Btn.SetIcon(&App1Iconimg);
			App1Btn.SetIconOver(&App1IconimgOver);
			App1Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App1IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App1Btn.SetImageOver(&App1BtnImgOver);
		App1Btn.SetSoundOver(&btnSoundOver);
		App1Btn.SetTrigger(&trigA);
		App1Btn.SetEffectGrow();
	}
	a++;
	

	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App2Icon(imgPath, 0);
	int ret2 = file_exists(imgPath);	
	GuiText App2BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App2BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App2BtnImg(&btnLargeOutline);
	GuiImage App2BtnImgOver(&btnLargeOutlineOver);
	GuiImage App2Iconimg(&App2Icon);
	GuiImage App2IconimgOver(&App2Icon);
	GuiButton App2Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App2Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App2Btn.SetPosition(328, 30);
	App2Btn.SetImage(&App2BtnImg);
	if (page == numpages && b > a)
	{
		if (ret2 == 0)
		{
			App2Btn.SetLabel(&App2BtnTxt);
		} else {
			App2Btn.SetIcon(&App2Iconimg);
			App2Btn.SetIconOver(&App2IconimgOver);
			App2Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App2IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App2Btn.SetImageOver(&App2BtnImgOver);
		App2Btn.SetSoundOver(&btnSoundOver);
		App2Btn.SetTrigger(&trigA);
		App2Btn.SetEffectGrow();
	}
	a++;
	
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App3Icon(imgPath, 0);
	int ret3 = file_exists(imgPath);
	GuiText App3BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App3BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App3BtnImg(&btnLargeOutline);
	GuiImage App3BtnImgOver(&btnLargeOutlineOver);
	GuiImage App3Iconimg(&App3Icon);
	GuiImage App3IconimgOver(&App3Icon);
	GuiButton App3Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App3Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App3Btn.SetPosition(482, 30);
	App3Btn.SetImage(&App3BtnImg);
	if (page == numpages && b > a)
	{
		if (ret3 == 0)
		{
			App3Btn.SetLabel(&App3BtnTxt);
		} else {
			App3Btn.SetIcon(&App3Iconimg);
			App3Btn.SetIconOver(&App3IconimgOver);
			App3Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App3IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App3Btn.SetImageOver(&App3BtnImgOver);
		App3Btn.SetSoundOver(&btnSoundOver);
		App3Btn.SetTrigger(&trigA);
		App3Btn.SetEffectGrow();
	}
	a++;
	
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App4Icon(imgPath, 0);
	int ret4 = file_exists(imgPath);
	GuiText App4BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App4BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App4BtnImg(&btnLargeOutline);
	GuiImage App4BtnImgOver(&btnLargeOutlineOver);
	GuiImage App4Iconimg(&App4Icon);
	GuiImage App4IconimgOver(&App4Icon);
	GuiButton App4Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App4Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App4Btn.SetPosition(174, 133);
	App4Btn.SetImage(&App4BtnImg);
	if (page == numpages && b > a)
	{
		if (ret4 == 0)
		{
			App4Btn.SetLabel(&App4BtnTxt);
		} else {
			App4Btn.SetIcon(&App4Iconimg);
			App4Btn.SetIconOver(&App4IconimgOver);
			App4Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App4IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App4Btn.SetImageOver(&App4BtnImgOver);
		App4Btn.SetSoundOver(&btnSoundOver);
		App4Btn.SetTrigger(&trigA);
		App4Btn.SetEffectGrow();
	}
	a++;
	
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App5Icon(imgPath, 0);
	int ret5 = file_exists(imgPath);
	GuiText App5BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App5BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App5BtnImg(&btnLargeOutline);
	GuiImage App5BtnImgOver(&btnLargeOutlineOver);
	GuiImage App5Iconimg(&App5Icon);
	GuiImage App5IconimgOver(&App5Icon);
	GuiButton App5Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App5Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App5Btn.SetPosition(328, 133);
	App5Btn.SetImage(&App5BtnImg);
	if (page == numpages && b > a)
	{
		if (ret5 == 0)
		{
			App5Btn.SetLabel(&App5BtnTxt);
		} else {
			App5Btn.SetIcon(&App5Iconimg);
			App5Btn.SetIconOver(&App5IconimgOver);
			App5Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App5IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App5Btn.SetImageOver(&App5BtnImgOver);
		App5Btn.SetSoundOver(&btnSoundOver);
		App5Btn.SetTrigger(&trigA);
		App5Btn.SetEffectGrow();
	}
	a++;
	
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App6Icon(imgPath, 0);
	int ret6 = file_exists(imgPath);
	GuiText App6BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App6BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App6BtnImg(&btnLargeOutline);
	GuiImage App6BtnImgOver(&btnLargeOutlineOver);
	GuiImage App6Iconimg(&App6Icon);
	GuiImage App6IconimgOver(&App6Icon);
	GuiButton App6Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App6Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App6Btn.SetPosition(482, 133);
	App6Btn.SetImage(&App6BtnImg);
	if (page == numpages && b > a)
	{
		if (ret6 == 0)
		{
			App6Btn.SetLabel(&App6BtnTxt);
		} else {
			App6Btn.SetIcon(&App6Iconimg);
			App6Btn.SetIconOver(&App6IconimgOver);
			App6Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App6IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App6Btn.SetImageOver(&App6BtnImgOver);
		App6Btn.SetSoundOver(&btnSoundOver);
		App6Btn.SetTrigger(&trigA);
		App6Btn.SetEffectGrow();
	}
	a++;
	
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App7Icon(imgPath, 0);
	int ret7 = file_exists(imgPath);
	GuiText App7BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App7BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App7BtnImg(&btnLargeOutline);
	GuiImage App7BtnImgOver(&btnLargeOutlineOver);
	GuiImage App7Iconimg(&App7Icon);
	GuiImage App7IconimgOver(&App7Icon);
	GuiButton App7Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App7Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App7Btn.SetPosition(20, 236);
	App7Btn.SetImage(&App7BtnImg);
	if (page == numpages && b > a)
	{
		if (ret7 == 0)
		{
			App7Btn.SetLabel(&App7BtnTxt);
		} else {
			App7Btn.SetIcon(&App7Iconimg);
			App7Btn.SetIconOver(&App7IconimgOver);
			App7Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App7IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App7Btn.SetImageOver(&App7BtnImgOver);
		App7Btn.SetSoundOver(&btnSoundOver);
		App7Btn.SetTrigger(&trigA);
		App7Btn.SetEffectGrow();
	}
	a++;
	
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App8Icon(imgPath, 0);
	int ret8 = file_exists(imgPath);
	GuiText App8BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App8BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App8BtnImg(&btnLargeOutline);
	GuiImage App8BtnImgOver(&btnLargeOutlineOver);
	GuiImage App8Iconimg(&App8Icon);
	GuiImage App8IconimgOver(&App8Icon);
	GuiButton App8Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App8Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App8Btn.SetPosition(174, 236);
	App8Btn.SetImage(&App8BtnImg);
	if (page == numpages && b > a)
	{
		if (ret8 == 0)
		{
			App8Btn.SetLabel(&App8BtnTxt);
		} else {
			App8Btn.SetIcon(&App8Iconimg);
			App8Btn.SetIconOver(&App8IconimgOver);
			App8Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App8IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App8Btn.SetImageOver(&App8BtnImgOver);
		App8Btn.SetSoundOver(&btnSoundOver);
		App8Btn.SetTrigger(&trigA);
		App8Btn.SetEffectGrow();
	}
	a++;
	
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App9Icon(imgPath, 0);
	int ret9 = file_exists(imgPath);
	GuiText App9BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App9BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App9BtnImg(&btnLargeOutline);
	GuiImage App9BtnImgOver(&btnLargeOutlineOver);
	GuiImage App9Iconimg(&App9Icon);
	GuiImage App9IconimgOver(&App9Icon);
	GuiButton App9Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App9Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App9Btn.SetPosition(328, 236);
	App9Btn.SetImage(&App9BtnImg);
	if (page == numpages && b > a)
	{
		if (ret9 == 0)
		{
			App9Btn.SetLabel(&App9BtnTxt);
		} else {
			App9Btn.SetIcon(&App9Iconimg);
			App9Btn.SetIconOver(&App9IconimgOver);
			App9Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App9IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App9Btn.SetImageOver(&App9BtnImgOver);
		App9Btn.SetSoundOver(&btnSoundOver);
		App9Btn.SetTrigger(&trigA);
		App9Btn.SetEffectGrow();
	} else {
		App9Btn.SetRumble(false);
	}
	a++;
	
	sprintf (imgPath, "%sicon.png", Settings[a].AppPath);
	GuiImageData App10Icon(imgPath, 0);
	int ret10 = file_exists(imgPath);
	GuiText App10BtnTxt(Settings[a].AppName, 22, (GXColor){0, 0, 0, 255});
	App10BtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage App10BtnImg(&btnLargeOutline);
	GuiImage App10BtnImgOver(&btnLargeOutlineOver);
	GuiImage App10Iconimg(&App10Icon);
	GuiImage App10IconimgOver(&App10Icon);
	GuiButton App10Btn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	App10Btn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	App10Btn.SetPosition(482, 236);
	App10Btn.SetImage(&App10BtnImg);
	if (page == numpages && b > a)
	{
		if (ret10 == 0)
		{
			App10Btn.SetLabel(&App10BtnTxt);
		} else {
			App10Btn.SetIcon(&App10Iconimg);
			App10Btn.SetIconOver(&App10IconimgOver);
			App10Iconimg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
			App10IconimgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		}
		App10Btn.SetImageOver(&App10BtnImgOver);
		App10Btn.SetSoundOver(&btnSoundOver);
		App10Btn.SetTrigger(&trigA);
		App10Btn.SetEffectGrow();
	}
	a++;
		
	GuiText DVDBtnTxt("DVD", 22, (GXColor){0,0,0,255});
	DVDBtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage DVDBtnImg(&btnLargeOutline);
	GuiImage DVDBtnImgOver(&btnLargeOutlineOver);
	GuiImage DVDIconImg(&btnDVD);
	GuiImage DVDIconImgOver(&btnDVDOver);
	GuiButton DVDBtn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	DVDBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	DVDBtn.SetPosition(20, 30);
	DVDBtn.SetLabel(&DVDBtnTxt);
	DVDBtn.SetImage(&DVDBtnImg);
	DVDBtn.SetImageOver(&DVDBtnImgOver);
	DVDBtn.SetIcon(&DVDIconImg);
	DVDBtn.SetIconOver(&DVDIconImg);
	DVDIconImg.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
	DVDIconImgOver.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
	DVDBtn.SetSoundOver(&btnSoundOver);
	DVDBtn.SetTrigger(&trigA);
	DVDBtn.SetEffectGrow();
	
	GuiText ChanBtnTxt("Channels", 22, (GXColor){0,0,0,255});
	ChanBtnTxt.SetMaxWidth(btnLargeOutline.GetWidth()-30);
	GuiImage ChanBtnImg(&btnLargeOutline);
	GuiImage ChanBtnImgOver(&btnLargeOutlineOver);
	GuiButton ChanBtn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	ChanBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	ChanBtn.SetPosition(20, 133);
	ChanBtn.SetLabel(&ChanBtnTxt);
	ChanBtn.SetImage(&ChanBtnImg);
	ChanBtn.SetImageOver(&ChanBtnImgOver);
	ChanBtn.SetSoundOver(&btnSoundOver);
	ChanBtn.SetTrigger(&trigA);
	ChanBtn.SetEffectGrow();
	
	
	GuiImage exitBtnImg(&btnExit);
	GuiImage exitBtnImgOver(&btnExitOver);
	GuiButton exitBtn(btnExit.GetWidth(), btnExit.GetHeight());
	exitBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	exitBtn.SetPosition(489, 347);
	exitBtn.SetImage(&exitBtnImg);
	exitBtn.SetImageOver(&exitBtnImgOver);
	exitBtn.SetSoundOver(&btnSoundOver);
	exitBtn.SetTrigger(&trigA);
	exitBtn.SetTrigger(&trigHome);
	exitBtn.SetEffectGrow();

	GuiImage SettingsBtnImg(&btnSettings);
	GuiImage SettingsBtnImgOver(&btnSettingsOver);
	GuiButton SettingsBtn(btnSettings.GetWidth(), btnSettings.GetHeight());
	SettingsBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	SettingsBtn.SetPosition(55, 347);
	SettingsBtn.SetImage(&SettingsBtnImg);
	SettingsBtn.SetImageOver(&SettingsBtnImgOver);
	SettingsBtn.SetSoundOver(&btnSoundOver);
	SettingsBtn.SetTrigger(&trigA);
	SettingsBtn.SetEffectGrow();

	HaltGui();
	GuiWindow w(screenwidth, screenheight);
	
	
	if (numpages > 1) {
		w.Append(&plusBtn);
		w.Append(&minusBtn);
	}
	
	w.Append(&App1Btn);
	w.Append(&App2Btn);
	w.Append(&App3Btn);
	w.Append(&App4Btn);
	w.Append(&App5Btn);
	w.Append(&App6Btn);
	w.Append(&App7Btn);
	w.Append(&App8Btn);
	w.Append(&App9Btn);
	w.Append(&App10Btn);
	w.Append(&timeTxt);
	w.Append(&dateTxt);
	w.Append(&ChanBtn);
	w.Append(&DVDBtn);
	w.Append(&exitBtn);
	w.Append(&SettingsBtn);
	mainWindow->Append(&w);

	ResumeGui();

	while(menu == MENU_NONE)
	{
		VIDEO_WaitVSync ();
		
				
		time ( &rawtime );
		timeinfo = localtime ( &rawtime );
		
		if (mintest != timeinfo->tm_min)
		{
			mintest = timeinfo->tm_min;
			strftime (curtime,80,"%I:%M%p",timeinfo);
			timeTxt.SetText(curtime);
		}
		if (daytest != timeinfo->tm_wday)
		{
			daytest = timeinfo->tm_wday;
			strftime (curdate,80,"%a %d/%m",timeinfo);
			dateTxt.SetText(curdate);
		}
		
		if(App1Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 10;
			menu = MENU_BUTTON;			
		}
		
		else if(App2Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 9;
			menu = MENU_BUTTON;
		}
		
				
		else if(App3Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 8;
			menu = MENU_BUTTON;
		}
		
		else if(App4Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 7;
			menu = MENU_BUTTON;		
		}
		else if(App5Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 6;
			menu = MENU_BUTTON;		
		}
		else if(App6Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 5;
			menu = MENU_BUTTON;		
		}
		else if(App7Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 4;
			menu = MENU_BUTTON;		
		}
		else if(App8Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 3;
			menu = MENU_BUTTON;		
		}
		else if(App9Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 2;
			menu = MENU_BUTTON;		
		}
		else if(App10Btn.GetState() == STATE_CLICKED)
		{
			appload = (page *10) - 1;
			menu = MENU_BUTTON;		
		}

		else if(DVDBtn.GetState() == STATE_CLICKED)
		{
			load = 2;
			menu = MENU_EXIT;
		}
	
		else if (ChanBtn.GetState() == STATE_CLICKED)
		{
			menu = MENU_CHAN;
		}
		
		else if(exitBtn.GetState() == STATE_CLICKED)
		{
			int et2 = WindowPrompt("Exit", "Are you sure you want to exit", "Yes", "No");
			if (et2 == 0){
				menu = MENU_MAIN;
			} else { 
				menu = MENU_EXIT; 
			}
		}
		
		else if(SettingsBtn.GetState() == STATE_CLICKED)
		{
			menu = MENU_SETTINGS;
		}
		
		else if (minusBtn.GetState() == STATE_CLICKED)
		{
			page--;
			if (page < 1)
				page = numpages;
			menu = MENU_MAIN;
				
		}
		
		else if (plusBtn.GetState() == STATE_CLICKED)
		{
			page++;
			if (page > numpages)
				page = 1;
			menu = MENU_MAIN;
		}
	}

	HaltGui();
	mainWindow->Remove(&w);
	return menu;
}


/****************************************************************************
 *SettingsMenu	
 ***************************************************************************/

static int SettingsMenu()
{
	
	snprintf(imgPath, sizeof(imgPath), "%sbutton.png", Settings[0].ThemePath);
	GuiImageData btnOutline(imgPath, 0);	
	snprintf(imgPath, sizeof(imgPath), "%sbutton_over.png", Settings[0].ThemePath);
	GuiImageData btnOutlineOver(imgPath, 0);
	
	int menu = MENU_NONE;
	int ret;
	int i = 0;
	int j = 0;
	
	OptionList options;
	
	sprintf(options.name[i++], "Theme Path");
	while (!strstr(Settings[j].AppName,"-1")) {
	
		sprintf(options.name[i++], "App%i Name", j);
		j++;
	}

	options.length = i;
	j = 0;
	
	sprintf(Settings[0].TempTheme, "%s", Settings[0].ThemePath);
	while (!strstr(Settings[j].AppName,"-1")) {
	
		sprintf(Settings[j].TempName, "%s", Settings[j].AppName);
		j++;
	}
	sprintf(Settings[j].TempName, "-1");
	
	snprintf(imgPath, sizeof(imgPath), "%sbattery_white.png", Settings[0].ThemePath);
	GuiImageData battery(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbattery_red.png", Settings[0].ThemePath);
	GuiImageData batteryRed(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbattery_bar_white.png", Settings[0].ThemePath);
	GuiImageData batteryBar(imgPath, 0);
		#ifdef HW_RVL
	int f = 0, level;
	char txt[3];
	GuiText * batteryTxt[4];
	GuiImage * batteryImg[4];
	GuiImage * batteryBarImg[4];
	GuiButton * batteryBtn[4];

	for(f=0; f < 4; f++)
	{

		if(f == 0)
			sprintf(txt, "P%d", f+1);
		else
			sprintf(txt, "P%d", f+1);

		batteryTxt[f] = new GuiText(txt, 22, (GXColor){255,255,255, 255});
		batteryTxt[f]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
		batteryImg[f] = new GuiImage(&battery);
		batteryImg[f]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
		batteryImg[f]->SetPosition(36, 0);
		batteryImg[f]->SetTile(0);
		batteryBarImg[f] = new GuiImage(&batteryBar);
		batteryBarImg[f]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
		batteryBarImg[f]->SetPosition(33, 0);

		batteryBtn[f] = new GuiButton(40, 20);
		batteryBtn[f]->SetLabel(batteryTxt[f]);
		batteryBtn[f]->SetImage(batteryBarImg[f]);
		batteryBtn[f]->SetIcon(batteryImg[f]);
		batteryBtn[f]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
		batteryBtn[f]->SetRumble(false);
		batteryBtn[f]->SetAlpha(70);
		batteryBtn[f]->SetEffect(EFFECT_SLIDE_BOTTOM | EFFECT_SLIDE_IN, 50);
	}

	batteryBtn[0]->SetPosition(153,195);
	batteryBtn[1]->SetPosition(247, 195);
	batteryBtn[2]->SetPosition(341, 195);
	batteryBtn[3]->SetPosition(435, 195);
    #endif

	GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, SOUND_PCM);
	

	snprintf(imgPath, sizeof(imgPath), "%ssetback.png", Settings[0].ThemePath);
	GuiImageData setback(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sok_button.png", Settings[0].ThemePath);
	GuiImageData btnOK(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sok_button_over.png", Settings[0].ThemePath);
	GuiImageData btnOKover(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%scancel_button.png", Settings[0].ThemePath);
	GuiImageData btncancel(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%scancel_button_over.png", Settings[0].ThemePath);
	GuiImageData btncancelover(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sadd.png", Settings[0].ThemePath);
	GuiImageData btnadd(imgPath, 0);

	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
	
	GuiImage settingsbackground(&setback);


	GuiImage backBtnImg(&btncancel);
	GuiImage backBtnImgOver(&btncancelover);
	GuiButton backBtn(btncancel.GetWidth(), btncancel.GetHeight());
	backBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	backBtn.SetPosition(489, 347);
	backBtn.SetImage(&backBtnImg);
	backBtn.SetImageOver(&backBtnImgOver);
	backBtn.SetSoundOver(&btnSoundOver);
	backBtn.SetTrigger(&trigA);
	backBtn.SetEffectGrow();
	

	GuiImage OKBtnImg(&btnOK);
	GuiImage OKBtnImgOver(&btnOKover);
	GuiButton OKBtn(btnOK.GetWidth(), btnOK.GetHeight());
	OKBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	OKBtn.SetPosition(55, 347);
	OKBtn.SetImage(&OKBtnImg);
	OKBtn.SetImageOver(&OKBtnImgOver);
	OKBtn.SetSoundOver(&btnSoundOver);
	OKBtn.SetTrigger(&trigA);
	OKBtn.SetEffectGrow();
	
	GuiText addBtnTxt("ADD", 22, (GXColor){0, 0, 0, 255});
	GuiImage addBtnImg(&btnadd);
	GuiImage addBtnImgOver(&btnadd);
	GuiButton addBtn(btnadd.GetWidth(), btnadd.GetHeight());
	addBtn.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
	addBtn.SetPosition(0, 320);
	addBtn.SetLabel(&addBtnTxt);
	addBtn.SetImage(&addBtnImg);
	addBtn.SetImageOver(&addBtnImgOver);
	addBtn.SetSoundOver(&btnSoundOver);
	addBtn.SetTrigger(&trigA);
	addBtn.SetEffectGrow();
	
	GuiOptionBrowser optionBrowser(552, 248, &options);
	optionBrowser.SetPosition(0, 60);
	optionBrowser.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
	optionBrowser.SetCol2Position(185);

	HaltGui();
	GuiWindow w(screenwidth, screenheight);
	#ifdef HW_RVL
	w.Append(batteryBtn[0]);
	w.Append(batteryBtn[1]);
	w.Append(batteryBtn[2]);
	w.Append(batteryBtn[3]);
	#endif
	w.Append(&addBtn);
	w.Append(&backBtn);
	w.Append(&OKBtn);
	mainWindow->Append(&settingsbackground);
	mainWindow->Append(&optionBrowser);
	mainWindow->Append(&w);
	ResumeGui();

	while(menu == MENU_NONE)
	{
		VIDEO_WaitVSync ();
		i = 0;
		j = 0;
		snprintf (options.value[i++], 256, "%s", Settings[j].TempTheme);

		while (!strstr(Settings[j].TempName,"-1")) {
		
			snprintf (options.value[i++], 256, "%s", Settings[j].TempName);
			j++;
		}
		#ifdef HW_RVL
		for(i=0; i < 4; i++)
		{
			if(WPAD_Probe(i, NULL) == WPAD_ERR_NONE) // controller connected
			{
				level = (userInput[i].wpad.battery_level / 100.0) * 4;
				if(level > 4) level = 4;
				batteryImg[i]->SetTile(level);

				if(level == 0)
					batteryBarImg[i]->SetImage(&batteryRed);
				else
					batteryBarImg[i]->SetImage(&batteryBar);

				batteryBtn[i]->SetAlpha(255);
			}
			else // controller not connected
			{
				batteryImg[i]->SetTile(0);
				batteryImg[i]->SetImage(&battery);
				batteryBtn[i]->SetAlpha(70);
			}
		}
		#endif
	
		ret = optionBrowser.GetClickedOption();
			
		if (ret != -1) {
			if (ret == 0) {
				OnScreenKeyboard(Settings[0].TempTheme, 256);
				menu = MENU_SETTINGS;
			} else {
				int l = ret - 1;
				int done = 2;
				while (done == 2) {
					done = ManagePrompt(Settings[l].AppName, Settings[l].AppPath, Settings[l].AppFile);
				}
				menu = MENU_SETTINGS;
			}
		}
		
				
		if(backBtn.GetState() == STATE_CLICKED)
		{
			menu = MENU_MAIN;
		}
		
		if(OKBtn.GetState() == STATE_CLICKED)
		{
			snprintf (Settings[0].ThemePath, 256, "%s", Settings[0].TempTheme);
			WriteConfig(conf);
			menu = MENU_MAIN;
		}
		
		if(addBtn.GetState() == STATE_CLICKED)
		{
			int b = 0;
			while (!strstr(Settings[b].AppName, "-1")) {
				b++;
			}
			WindowPrompt("New Name", "Enter new applications name on the next screen", "Continue", NULL);
			OnScreenKeyboard(Settings[b].AppName, 256);
			WindowPrompt("New Path", "Enter new applications Path on the next screen", "Continue", NULL);
			OnScreenKeyboard(Settings[b].AppPath, 256);
			WindowPrompt("New File Name", "Enter new applications file name on the next screen", "Continue", NULL);
			OnScreenKeyboard(Settings[b].AppFile, 256);
			b++;
			sprintf (Settings[b].AppName, "-1");
			WriteConfig(conf);
			menu = MENU_SETTINGS;			
		}

				
	}
	HaltGui();
	mainWindow->Remove(&settingsbackground);
	mainWindow->Remove(&optionBrowser);
	mainWindow->Remove(&w);
	return menu;
}

/****************************************************************************
 *ButtonMenu	
 ***************************************************************************/

static int ButtonMenu()
{
	
	snprintf(imgPath, sizeof(imgPath), "%sbutton.png", Settings[0].ThemePath);
	GuiImageData btnOutline(imgPath, 0);	
	snprintf(imgPath, sizeof(imgPath), "%sbutton_over.png", Settings[0].ThemePath);
	GuiImageData btnOutlineOver(imgPath, 0);
	
	int menu = MENU_NONE;
	
	GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, SOUND_PCM);
	

	snprintf(imgPath, sizeof(imgPath), "%sbuttonback.png", Settings[0].ThemePath);
	GuiImageData buttonback(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%siconlarge.png", Settings[appload].AppPath);
	GuiImageData buttonicon(imgPath, 0);
	
	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
	GuiTrigger trigPlus;
	trigPlus.SetButtonOnlyTrigger(-1, WPAD_BUTTON_PLUS | WPAD_CLASSIC_BUTTON_PLUS, 0);
	GuiTrigger trigMinus;
	trigMinus.SetButtonOnlyTrigger(-1, WPAD_BUTTON_MINUS | WPAD_CLASSIC_BUTTON_MINUS, 0);
	
	GuiImage buttonbackground(&buttonback);
	GuiImage buttoniconback(&buttonicon);
	buttoniconback.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
	
	GuiText titleTxt(Settings[appload].AppName, 26, (GXColor){255, 255, 255, 255});
	titleTxt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
	titleTxt.SetPosition(0,40);

	GuiButton plusBtn(0,0);
	plusBtn.SetTrigger(&trigPlus);
	
	GuiButton minusBtn(0,0);
	minusBtn.SetTrigger(&trigMinus);
	
	GuiText backTxt("Wii Menu", 22, (GXColor){0,0,0,255});
	GuiImage backBtnImg(&btnOutline);
	GuiImage backBtnImgOver(&btnOutlineOver);
	GuiButton backBtn(btnOutline.GetWidth(), btnOutline.GetHeight());
	backBtn.SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
	backBtn.SetLabel(&backTxt);
	backBtn.SetPosition(-25, 400);
	backBtn.SetImage(&backBtnImg);
	backBtn.SetImageOver(&backBtnImgOver);
	backBtn.SetSoundOver(&btnSoundOver);
	backBtn.SetTrigger(&trigA);
	backBtn.SetEffectGrow();
	

	GuiText okTxt("Play", 22, (GXColor){0,0,0,255});
	GuiImage OKBtnImg(&btnOutline);
	GuiImage OKBtnImgOver(&btnOutlineOver);
	GuiButton OKBtn(btnOutline.GetWidth(), btnOutline.GetHeight());
	OKBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	OKBtn.SetPosition(25, 400);
	OKBtn.SetLabel(&okTxt);
	OKBtn.SetImage(&OKBtnImg);
	OKBtn.SetImageOver(&OKBtnImgOver);
	OKBtn.SetSoundOver(&btnSoundOver);
	OKBtn.SetTrigger(&trigA);
	OKBtn.SetEffectGrow();
	
	GuiText settingsBtnTxt("Settings", 22, (GXColor){0, 0, 0, 255});
	GuiImage settingsBtnImg(&btnOutline);
	GuiImage settingsBtnImgOver(&btnOutlineOver);
	GuiButton settingsBtn(btnOutline.GetWidth(), btnOutline.GetHeight());
	settingsBtn.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
	settingsBtn.SetPosition(0, 400);
	settingsBtn.SetLabel(&settingsBtnTxt);
	settingsBtn.SetImage(&settingsBtnImg);
	settingsBtn.SetImageOver(&settingsBtnImgOver);
	settingsBtn.SetSoundOver(&btnSoundOver);
	settingsBtn.SetTrigger(&trigA);
	settingsBtn.SetEffectGrow();
	
	HaltGui();
	GuiWindow w(screenwidth, screenheight);
	w.Append(&settingsBtn);
	w.Append(&backBtn);
	w.Append(&OKBtn);
	w.Append(&titleTxt);
	mainWindow->Append(&buttoniconback);
	mainWindow->Append(&buttonbackground);
	mainWindow->Append(&w);
	ResumeGui();

	while(menu == MENU_NONE)
	{
		VIDEO_WaitVSync ();
		
		if(plusBtn.GetState() == STATE_CLICKED)
		{
			appload = appload +1;
			menu = MENU_BUTTON;
		}
		
		else if(minusBtn.GetState() == STATE_CLICKED)
		{
			appload = appload -1;
			menu = MENU_BUTTON;
		}
		else if(backBtn.GetState() == STATE_CLICKED)
		{
			menu = MENU_MAIN;
		}
		
		else if(OKBtn.GetState() == STATE_CLICKED)
		{
			sprintf(Settings[appload].TempPath, "%s%s", Settings[appload].AppPath, Settings[appload].AppFile);
			path = Settings[appload].TempPath;
			load = 1;
			menu = MENU_EXIT;
		}
		
		else if(settingsBtn.GetState() == STATE_CLICKED)
		{
			ManagePrompt(Settings[appload].AppName, Settings[appload].AppPath, Settings[appload].AppFile);
			menu = MENU_BUTTON;
		}

				
	}
	HaltGui();
	mainWindow->Remove(&buttonbackground);
	mainWindow->Remove(&buttoniconback);
	mainWindow->Remove(&w);
	return menu;
}


/****************************************************************************
 *Channel Menu	
 ***************************************************************************/

static int ChannelMenu()
{
	
	snprintf(imgPath, sizeof(imgPath), "%sbutton.png", Settings[0].ThemePath);
	GuiImageData btnOutline(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%sbutton_over.png", Settings[0].ThemePath);
	GuiImageData btnOutlineOver(imgPath, 0);
	s32 ret;
	Channel_Gettitle();
	int menu = MENU_NONE;
	
	ChannelList channels;
	if (!channel_list[0].tid)
	{
		sprintf(channels.name[0], "No Channels Found");
	}
	

	channels.length = channel_cnt;


	GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, SOUND_PCM);
	snprintf(imgPath, sizeof(imgPath), "%ssetback.png", Settings[0].ThemePath);
	GuiImageData setback(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%scancel_button.png", Settings[0].ThemePath);
	GuiImageData btncancel(imgPath, 0);
	snprintf(imgPath, sizeof(imgPath), "%scancel_button_over.png", Settings[0].ThemePath);
	GuiImageData btncancelover(imgPath, 0);


	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
	
	GuiImage settingsbackground(&setback);
	GuiButton settingsbackgroundbtn(settingsbackground.GetWidth(), settingsbackground.GetHeight());
	settingsbackgroundbtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	settingsbackgroundbtn.SetPosition(0, 0);
	settingsbackgroundbtn.SetImage(&settingsbackground);

	GuiImage backBtnImg(&btncancel);
	GuiImage backBtnImgOver(&btncancelover);
	GuiButton backBtn(btncancel.GetWidth(), btncancel.GetHeight());
	backBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
	backBtn.SetPosition(489, 347);
	backBtn.SetImage(&backBtnImg);
	backBtn.SetImageOver(&backBtnImgOver);
	backBtn.SetSoundOver(&btnSoundOver);
	backBtn.SetTrigger(&trigA);
	backBtn.SetEffectGrow();

	
	GuiChannelBrowser channelBrowser(552, 248, &channels);
	channelBrowser.SetPosition(0, 60);
	channelBrowser.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);

	HaltGui();
	GuiWindow w(screenwidth, screenheight);
	w.Append(&backBtn);
	mainWindow->Append(&settingsbackground);
	mainWindow->Append(&channelBrowser);
	mainWindow->Append(&w);
	ResumeGui();

	while(menu == MENU_NONE)
	{
		VIDEO_WaitVSync ();
		u32 cnt = 0;

		while (cnt < channel_cnt) {
		
			snprintf (channels.name[cnt], 256, "%016llx", channel_list[cnt].tid);
			//strcpy(channels.name[cnt], channel_list[cnt].name);
			cnt++;
		}
			
		ret = channelBrowser.GetClickedOption();
			
				
		if (ret != -1)
		{
			//char* lan = NULL;
			//sprintf(lan, "Do you want to launch channel %016llx?", channel_list[ret].tid);
			//int et2 = WindowPrompt("Launch Channel", lan, "Yes", "NO");
			//if (et2 == 0){
				selected = ret;
				load = 3;
				menu = MENU_EXIT;
			//}
			//ret = -1;
		}
			
		if(backBtn.GetState() == STATE_CLICKED)
		{
			menu = MENU_MAIN;
		}
		
	}
	HaltGui();
	mainWindow->Remove(&settingsbackground);
	mainWindow->Remove(&channelBrowser);
	mainWindow->Remove(&w);
	return menu;
}


/****************************************************************************
 *	Menu Logic
 ***************************************************************************/
void FirstMenu(int menu)
{
	int currentMenu = menu;
	char mainbackimg[100];
	char play1img[100];
	char play2img[100];
	char play3img[100];
	char play4img[100];
	char oggpath[100];
	
	snprintf(play1img, sizeof(play1img), "%splayer1_point.png", Settings[0].ThemePath);
	snprintf(play2img, sizeof(play2img), "%splayer2_point.png", Settings[0].ThemePath);
	snprintf(play3img, sizeof(play3img), "%splayer3_point.png", Settings[0].ThemePath);
	snprintf(play4img, sizeof(play4img), "%splayer4_point.png", Settings[0].ThemePath);

	#ifdef HW_RVL
	pointer[0] = new GuiImageData(play1img, 0);
	pointer[1] = new GuiImageData(play2img, 0);
	pointer[2] = new GuiImageData(play3img, 0);
	pointer[3] = new GuiImageData(play4img, 0);
	#endif

	mainWindow = new GuiWindow(screenwidth, screenheight);
	snprintf (mainbackimg, sizeof(mainbackimg), "%sbackground.png", Settings[0].ThemePath);
	background = new GuiImageData(mainbackimg, 0);

	bgImg = new GuiImage(background);
	mainWindow->Append(bgImg);

	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
	
	
	ResumeGui();

	bgMusic = new GuiSound(oggpath, SOUND_OGG);
	sprintf(oggpath, "%s/bg_music.ogg", Settings[0].ThemePath);
	bgMusic->SetVolume(50);
	bgMusic->PlayOggFile(oggpath);

	while(currentMenu != MENU_EXIT)
	{
		switch (currentMenu)
		{
			case MENU_MAIN:
				currentMenu = MainMenu();
				break;
			case MENU_SETTINGS:
				currentMenu = SettingsMenu();
				break;
			case MENU_CHAN:
				currentMenu = ChannelMenu();
				break;
			case MENU_BUTTON:
				currentMenu = ButtonMenu();
				break;
			default: // unrecognized menu
				currentMenu = MainMenu();
				break;
		}
	}

	ResumeGui();
	ExitRequested = 1;
	while(1) usleep(50);

	HaltGui();

	bgMusic->Stop();
	delete bgMusic;
	delete bgImg;
	delete mainWindow;

	delete pointer[0];
	delete pointer[1];
	delete pointer[2];
	delete pointer[3];

	mainWindow = NULL;
	
	return;
}
